package com.bc.bcbuyerapihttp.controller;

import com.bc.bcapi.dao.T_File_upload_logMapper;
import com.bc.bcbuyerapihttp.common.ResultModel;
import com.bc.bccore.webconfig.WebConfig;
import com.bc.bcmodel.domain.T_File_upload_logModel;
import com.bc.commoncore.util.HttpUtil;
import com.bc.commoncore.util.JsonUtil;
import com.bc.commoncore.util.StringUtils;
import com.bc.commonentity.common.JsonResult;
import com.bc.commonentity.enums.HttpReturnRnums;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;

@Api("附件上传")
@RestController
@RequestMapping("uploadfile")
public class UploadFileController {

    @Autowired
    T_File_upload_logMapper t_file_upload_logMapper;

    @ApiOperation("附件上传")
    @RequestMapping(value = "upload", method = RequestMethod.POST)
    public JsonResult upload(String file_type,
                             MultipartFile file,
                             HttpServletResponse response) throws Exception {
        JsonResult js = new JsonResult();
        String url = WebConfig.getUploadUrl();
        HashMap<String, String> param = new HashMap<>();
        if (file_type.length()<5){
            file_type="project";
        }
        param.put("file_type", file_type);
        /**
         * check
         * */
        long filesize = file.getSize();
        if (filesize <= 0) {
            return new JsonResult(HttpReturnRnums.ParaError);
        }
        //10M大小限制
        if (filesize >= 10000000) {
            return new JsonResult(HttpReturnRnums.FILE_MAXLENGTH);
        }
        String filename = file.getOriginalFilename();
        String ext = filename.substring(filename.lastIndexOf("."));
        if (StringUtils.isEmpty(ext)) {
            return new JsonResult(HttpReturnRnums.ParaError);
        }
        ext = ext.toLowerCase();
        String fileExt = ".gif,.jpg,.jpeg,.png,.bmp,.pdf,.rar,.zip,.doc,.docx,.xls,.xlsx,.ppt,.pptx";
        if (!fileExt.contains(ext)) {
            return new JsonResult(HttpReturnRnums.FILE_FORMAT_ERROR);
        }
        String upResult = HttpUtil.sendPostAndUpload(url, param, file.getOriginalFilename(), file.getInputStream());
        upResult = upResult.replace('\'', '\"');
        if (StringUtils.isEmpty(upResult)) {
            return new JsonResult(HttpReturnRnums.UPLOAD_NON);
        }
        ResultModel model = JsonUtil.str2obj(upResult, ResultModel.class);
        if (model == null) {
            return new JsonResult(HttpReturnRnums.UPLOAD_NON);
        }
        String dataUrl = model.getDataUrl();
        if (StringUtils.isEmpty(dataUrl)) {
            return new JsonResult(HttpReturnRnums.UPLOAD_NON);
        }
        T_File_upload_logModel t_file_upload_logModel = new T_File_upload_logModel();
        t_file_upload_logModel.setSource_file_name(file.getOriginalFilename());
        String insertDataUrl = dataUrl.replace(WebConfig.getImgUrl(), "");
        t_file_upload_logModel.setFile_src(insertDataUrl);
        t_file_upload_logMapper.insert(t_file_upload_logModel);
        js.setData(dataUrl);
        return js;
    }
}
